package com.donghuayuan.kotlincar.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.donghuayuan.kotlincar.R
import com.donghuayuan.kotlincar.bean.Result
import com.donghuayuan.kotlincar.databinding.BrandItemBinding

class BrandAdapter(var context: Context, var mList: MutableList<Result>) :
    RecyclerView.Adapter<BrandAdapter.BrandHold>() {

    val inflater = LayoutInflater.from(context)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BrandHold {
        val binding: BrandItemBinding =
            DataBindingUtil.inflate(inflater, R.layout.brand_item, parent, false)
        val hold = BrandHold(binding.root)
        hold.binding = binding
        return hold
    }

    override fun onBindViewHolder(holder: BrandHold, position: Int) {
        val bean = mList[position]
        holder.binding?.item = bean
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    fun addList(result: MutableList<Result>?) {
        if (result!=null){
            mList.clear()
            mList.addAll(result)
            notifyDataSetChanged()
        }

    }

    class BrandHold(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding: BrandItemBinding? = null
    }
}